﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using QuanLyKho.Common;
using QuanLyKho.DataAccess;
using QuanLyKho.GUI.Library;

namespace QuanLyKho.GUI
{
    public partial class frmListWard : Form
    {
        private static long ID = 0;
        public frmListWard()
        {
            InitializeComponent();
        }

        private void frmListWard_Load(object sender, EventArgs e)
        {
            LoadDatacombo();
        }

        public frmListWard(long _ID)
        {
            InitializeComponent();
            txtCode.Focus();
            ID = _ID;
            binDataOneWard(_ID);
        }

        private void binDataOneWard(long _ID)
        {
            List<clsWard> listWard = new clsDataAccessListWard().ListWardDistrinctSelectOneWithId(_ID);
            if (listWard.Count > 0)
            {
                txtCode.Text = listWard[0].Code;
                txtName.Text = listWard[0].Name;
                txtShortName.Text = listWard[0].ShortName;
                txtDes.Text = listWard[0].Des;
                //cboIDlistWard.SelectedText = listWard[0].Nameprovince.ToString();
            }
        }

        public void LoadDatacombo()
        {
            try
            {
                List<clsDistrict> listDistrict = new clsDataAccessListDistrict().ListDistrictSelectAll();
                DataTable dt = new DataTable("Ward");
                dt.Columns.Add("ID", typeof(long));
                dt.Columns.Add("Name", typeof(string));
                DataRow dr;
                foreach (clsDistrict obj in listDistrict)
                {
                    dr = dt.NewRow();
                    dr[0] = obj.ID;
                    dr[1] = obj.Name;
                    dt.Rows.Add(dr);
                }
                cboIDlistWard.DataSource = dt;
                cboIDlistWard.DisplayMember = dt.Columns["Name"].ToString();
                cboIDlistWard.ValueMember = dt.Columns["ID"].ToString();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public void refesh()
        {
            txtCode.Text = "";
            txtName.Text = "";
            txtShortName.Text = "";
            txtDes.Text = "";
        }
        private void butGhilai_Click(object sender, EventArgs e)
        {
            string Code = txtCode.Text;
            string Name = txtName.Text;
            string ShortName = txtShortName.Text;
            string Des = txtDes.Text;
            long IDlistDistrict = Convert.ToInt64(cboIDlistWard.SelectedValue);
            if (Code.Length < 1)
            {
                new clsLibrary().runLoading("Mã không được trống.", 0);
                txtCode.Focus();
                return;
            }
            else
            {
                if (Code.Length > 255)
                {
                    new clsLibrary().runLoading("Mã không lớn hơn 255 ký tự.", 0);
                    txtCode.Focus();
                    return;
                }
            }

            if (Name.Length < 1)
            {
                new clsLibrary().runLoading("Họ tên không được trống.", 0);
                txtName.Focus();
                return;
            }
            else
            {
                if (Name.Length > 255)
                {
                    new clsLibrary().runLoading("Họ tên không lớn hơn 255 ký tự.", 0);
                    txtName.Focus();
                    return;
                }
            }
            clsWard obj = new clsWard();
            obj.Code = Code;
            obj.Name = Name;
            obj.ShortName = ShortName;
            obj.Des = Des;
            obj.IDlistDistrict = IDlistDistrict;
            if (ID < 1)
            {
                if (new clsDataAccessListWard().AddNew(obj) > 0)
                {
                    new clsLibrary().runLoading("Đã thêm mới thành công:", 2);
                    refesh();
                }
                else
                    new clsLibrary().runLoading("Có lỗi xảy ra khi thêm mới.", 0);
            }
            else
            {
                obj.ID = ID;
                if (new clsDataAccessListWard().Update(obj) > 0)
                {
                    new clsLibrary().runLoading("Đã sửa thành công!", 2);
                    refesh();
                    //this.Close();
                    ID = 0;
                }
                else
                    new clsLibrary().runLoading("Có lỗi xảy ra khi sửa.", 0);
            }
        }
        
        private void butAddNew_Click(object sender, EventArgs e)
        {
            refesh();
        }

        private void butExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
